local ToggleOnNextStrike = ToggleOnNextStrike_Get_Instance();


local L = ToggleOnNextStrike.L;

function ToggleOnNextStrike:CommandLine(cl)
	if type(cl) ~= "string" then
		self:Usage()
		return
	end
	local command = cl
	local params = ""
	local spaceIndex = string.find(cl, "%s")
	if spaceIndex then 
		command = strsub(cl, 1, spaceIndex-1) 
		--TODO: find end of spaces instead of just assuming one
		params = strsub(cl, spaceIndex+1)
	end
	local lcommand = string.lower(command)
	if lcommand == L("addmacro") or lcommand == L("am") then
		self:CLAddMacro(params)
	elseif lcommand == L("removemacro") or lcommand == L("delmacro") or lcommand == L("deletemacro") or lcommand == L("rm") or lcommand == L("dm") then
		self:CLRemoveMacro(params)
	elseif lcommand == L("scan") then
		self:CLScanMacro()
	elseif lcommand == L("list") then
		self:CLListMacro()
	elseif lcommand == L("toggle") then
		self:PerformToggle()
	elseif lcommand == L("enable") then
		self:PerformToggle(true)
	elseif lcommand == L("disable") then
		self:PerformToggle(false)
	else
		self:Error("TONS: unknown command - '"..command.."'")
		self:Usage()
	end
end

function ToggleOnNextStrike:CommandLineInit()
	local name = "TOGGLEONNEXTSTRIKE";
	local cmds = L("SlashCommands");
	if type(cmds) ~= "table" then
		if type(cmds) == "nil" then
			return false
		end
		local t = {}
		t.insert(cmds)
		cmds = t
	end
	local baseSlash = "SLASH_%s%d"
	for k,v in pairs(cmds) do
		setglobal(string.format(baseSlash, name, k), v)
	end
	SlashCmdList[name] = function(...) return self:CommandLine(...) end
end


function ToggleOnNextStrike:AddMacroToListByIndex(macroIndex)
	local n=GetMacroInfo(macroIndex)
	if type(n) == "string" then
		self:AddMacroToList(n)
	end
end

function ToggleOnNextStrike:AddMacroToList(macroName)
	local curList = self:GetSetting("macroList")
	if type(curList) ~= "table" then
		curList = {}
		self:SetSetting("macroList", curList)
	end
	if type(macroName) == "string" then
		curList[macroName] = true
		self:SetSetting("macroList", curList)
	end
end

function ToggleOnNextStrike:PerformMacroScan(func)
	local curList = self:GetSetting("macroList")
	if type(curList) == "table" then
		for k,v in pairs(curList) do
			curList[k] = nil
		end
		self:SetSetting("macroList", curList)
	end
	if type(func) == "nil" then func = "AddMacroToListByIndex" end
	self:ScanMacros(self.setup.abilityList, func)
end

function ToggleOnNextStrike:ScanMacros(listOfStrings, func)
	if type(listOfStrings) ~= "table" then 
		return false; 
	end
	local f = func;
	if type(f) == "string" then 
		if type(self[func]) == "function" then
			f = function(...) return self[func](self, ...) end
		else
			return false
		end
	end
	if type(f) ~= "function" then
		return false
	end
	-- TODO: Change this to iterate through all macros.
	
	-- GetNumMacros(); and use MAX_ACCOUNT_MACROS = 36
	for i = self:GetMaximumMacroId(), 1, -1 do
		local mt=GetMacroBody(i)
		if type(mt) == "string" and strlen(mt) > 0 then
			for k, v in pairs(listOfStrings) do
				if string.find(mt, v, 1, true) then 
					f(i) 
				end
			end
		end
	end
end

function ToggleOnNextStrike:Usage()
	local usg = L("Usage")
	if type(usg) == "table" then
		for k,v in pairs(usg) do
			self:Message(v)
		end
	elseif type(usg) == "nil" then
		self:Error("TONS: Usage info not found.")
	else
		self:Message(usg)
	end
end


function ToggleOnNextStrike:CLAddMacro(params)
	local macroId = self:FindMacroId(params)
	if type(macroId) ~= "nil" then
		self:AddMacroToList(params)
		self:Message("TONS: Added macro "..params)
	else
		if type(params) ~= "string" then params = "<nonstring>" end
		self:Error("TONS: Could not add macro "..params)
	end
end

function ToggleOnNextStrike:CLRemoveMacro(params)
	if type(params) == "nil" then
		self:Error("TONS: Need to specify a macro to remove")
		return
	end
	if not self:IsInMacroList(params) then
		if type(params) ~= "string" then params = "<nonstring>" end
		self:Error(string.format("TONS: Can not find macro %s to remove", params))
	else
		self:RemoveMacro(params)
		self:Message("TONS: Removed macro "..params)
	end
end

function ToggleOnNextStrike:IsInMacroList(macroName)
	local curList = self:GetSetting("macroList")
	if type(curList) ~= "table" then
		return false
	end
	if type(curList[macroName]) ~= "nil" then
		return true
	else
		return false
	end
end

function ToggleOnNextStrike:RemoveMacro(macroName)
	local curList = self:GetSetting("macroList")
	if type(curList) ~= "table" then
		return
	end
	if type(macroName) ~= "nil" and curList[macroName] then
		curList[macroName] = nil
		self:SetSetting("macroList", curList)
		return true
	end
end

function ToggleOnNextStrike:CLScanMacroMessage(macroIndex)
	local name = GetMacroInfo(macroIndex)
	if type(name) == "string" then
		self:AddMacroToList(name)
		self:Message("... added "..name)
	end
end

function ToggleOnNextStrike:CLScanMacro()
	if type(self.setup.ability) ~= "nil" then
		self:Message("TONS: Performing macro scan (all previous macros will be removed)");
		self:PerformMacroScan("CLScanMacroMessage")
	end
end

function ToggleOnNextStrike:CLListMacro()
	local curList = self:GetSetting("macroList") 
	local noMacros = true
	if type(curList) == "table" then
		for k,v in pairs(curList) do
			if v then
				if noMacros then
					self:Message("TONS: List of macros being toggled")
				end
				noMacros = false
				self:Message(k)
			end
		end
	end
	if noMacros then
		self:Message("TONS: No macros listed.");
	end
end

ToggleOnNextStrike:CommandLineInit();
